package com.hunttown.mes.manage.service;

import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.rpc.domain.AnalysisManageRightRoleAdminRelDTO;
import com.hunttown.mes.rpc.api.AnalysisManageRightRoleAdminRelRpcService;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightRoleAdminRelDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.hunttown.mes.common.utils.FormatUtils.formatCommas;

/**
 * created by hunttown on 2018-10-26 15:04:57
 */
@Service
public class AnalysisManageRightRoleAdminRelManageService {

    private final static Logger logger = LoggerFactory.getLogger(AnalysisManageRightRoleAdminRelManageService.class);

    private final AnalysisManageRightRoleAdminRelRpcService rpcService;

    @Autowired
    public AnalysisManageRightRoleAdminRelManageService(AnalysisManageRightRoleAdminRelRpcService rpcService) {
        this.rpcService = rpcService;
    }

    // 新增
    public AnalysisManageRightRoleAdminRelDTO insert(AnalysisManageRightRoleAdminRelDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(AnalysisManageRightRoleAdminRelDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(AnalysisManageRightRoleAdminRelDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        return rpcService.deleteById(id);
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        AnalysisManageRightRoleAdminRelDTOQuery query = new AnalysisManageRightRoleAdminRelDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public AnalysisManageRightRoleAdminRelDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public AnalysisManageRightRoleAdminRelDTO getByQuery(AnalysisManageRightRoleAdminRelDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    public AnalysisManageRightRoleAdminRelDTO getByAdminRole(Integer adminId, Integer roleId) {
        AnalysisManageRightRoleAdminRelDTOQuery query = new AnalysisManageRightRoleAdminRelDTOQuery();
        query.setAdminId(adminId);
        query.setRoleId(roleId);
        query.setDeleteFlag(0);
        AnalysisManageRightRoleAdminRelDTO dto = getByQuery(query);
        return dto;
    }

    // 获取分页列表
    public Page<AnalysisManageRightRoleAdminRelDTO> getForPage(AnalysisManageRightRoleAdminRelDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    //批量插入
    public void insrtForBatch(Integer manageId, String roleIds) {
        AnalysisManageRightRoleAdminRelDTO obj = null;
        List<AnalysisManageRightRoleAdminRelDTO> list = new ArrayList<>();

        if (manageId <= 0) {
            return;
        }

        //处理字符串中的","
        roleIds = formatCommas(roleIds);

        if (roleIds == null || roleIds == "") {
            return;
        }

        String[] ids = roleIds.split(",");
        for (String item : ids) {
            if (item.equals("on")) {
                continue;
            }

            obj = new AnalysisManageRightRoleAdminRelDTO();
            obj.setAdminId(manageId);
            obj.setRoleId(Integer.valueOf(item));
            obj.setDeleteFlag(0);

            list.add(obj);
        }

        rpcService.insertForBatch(list);
    }

    //删除
    //请注意：此方法是通过manage_id 进行删除操作的。
    public Boolean delete(String transferIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(transferIds);

        if (idList.size() == 0) {
            return false;
        }

        AnalysisManageRightRoleAdminRelDTOQuery query = new AnalysisManageRightRoleAdminRelDTOQuery();
        query.setDeleteFlag(1);
        query.setAdminIds(idList);   //这个地方传数组JSF报错：Expected map, but got array value.

        return rpcService.updateInfoByQuery(query);
    }
}